1
2
3
4
5
6
7 package io.vavr.collection;
8
9 import io.vavr.*;
10 import io.vavr.control.Option;
11
12 import java.util.*;
13 import java.util.function.*;
14 import java.util.stream.Collector;
15
16 import static io.vavr.collection.JavaConverters.ChangePolicy.IMMUTABLE;
17 import static io.vavr.collection.JavaConverters.ChangePolicy.MUTABLE;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public final class Queue<T> extends AbstractQueue<T, Queue<T>> implements LinearSeq<T> {
46
47 private static final long serialVersionUID = 1L;
48
49 private static final Queue<?> EMPTY = new Queue<>(io.vavr.collection.List.empty(), io.vavr.collection.List.empty());
50
51 private final io.vavr.collection.List<T> front;
52 private final io.vavr.collection.List<T> rear;
53
54
55
56
57
58
59
60
61
62
63 private Queue(io.vavr.collection.List<T> front, io.vavr.collection.List<T> rear) {
64 final boolean frontIsEmpty = front.isEmpty();
65 this.front = frontIsEmpty ? rear.reverse() : front;
66 this.rear = frontIsEmpty ? front : rear;
67 }
68
69
70
71
72
73
74
75
76
77 public static <T> Collector<T, ArrayList<T>, Queue<T>> collector() {
78 final Supplier<ArrayList<T>> supplier = ArrayList::new;
79 final BiConsumer<ArrayList<T>, T> accumulator = ArrayList::add;
80 final BinaryOperator<ArrayList<T>> combiner = (left, right) -> {
81 left.addAll(right);
82 return left;
83 };
84 final Function<ArrayList<T>, Queue<T>> finisher = Queue::ofAll;
85 return Collector.of(supplier, accumulator, combiner, finisher);
86 }
87
88
89
90
91
92
93
94 @SuppressWarnings("unchecked")
95 public static <T> Queue<T> empty() {
96 return (Queue<T>) EMPTY;
97 }
98
99
100
101
102
103
104
105
106
107
108 @SuppressWarnings("unchecked")
109 public static <T> Queue<T> narrow(Queue<? extends T> queue) {
110 return (Queue<T>) queue;
111 }
112
113
114
115
116
117
118
119
120 public static <T> Queue<T> of(T element) {
121 return ofAll(io.vavr.collection.List.of(element));
122 }
123
124
125
126
127
128
129
130
131
132 @SuppressWarnings("varargs")
133 @SafeVarargs
134 public static <T> Queue<T> of(T... elements) {
135 Objects.requireNonNull(elements, "elements is null");
136 return ofAll(io.vavr.collection.List.of(elements));
137 }
138
139
140
141
142
143
144
145
146
147 @SuppressWarnings("unchecked")
148 public static <T> Queue<T> ofAll(Iterable<? extends T> elements) {
149 Objects.requireNonNull(elements, "elements is null");
150 if (elements instanceof Queue) {
151 return (Queue<T>) elements;
152 } else if (!elements.iterator().hasNext()) {
153 return empty();
154 } else if (elements instanceof io.vavr.collection.List) {
155 return new Queue<>((io.vavr.collection.List<T>) elements, io.vavr.collection.List.empty());
156 } else {
157 return new Queue<>(io.vavr.collection.List.ofAll(elements), io.vavr.collection.List.empty());
158 }
159 }
160
161
162
163
164
165
166
167
168 public static <T> Queue<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
169 Objects.requireNonNull(javaStream, "javaStream is null");
170 return new Queue<>(io.vavr.collection.List.ofAll(javaStream), io.vavr.collection.List.empty());
171 }
172
173
174
175
176
177
178
179
180 public static Queue<Boolean> ofAll(boolean... elements) {
181 Objects.requireNonNull(elements, "elements is null");
182 return ofAll(io.vavr.collection.List.ofAll(elements));
183 }
184
185
186
187
188
189
190
191
192 public static Queue<Byte> ofAll(byte... elements) {
193 Objects.requireNonNull(elements, "elements is null");
194 return ofAll(io.vavr.collection.List.ofAll(elements));
195 }
196
197
198
199
200
201
202
203
204 public static Queue<Character> ofAll(char... elements) {
205 Objects.requireNonNull(elements, "elements is null");
206 return ofAll(io.vavr.collection.List.ofAll(elements));
207 }
208
209
210
211
212
213
214
215
216 public static Queue<Double> ofAll(double... elements) {
217 Objects.requireNonNull(elements, "elements is null");
218 return ofAll(io.vavr.collection.List.ofAll(elements));
219 }
220
221
222
223
224
225
226
227
228 public static Queue<Float> ofAll(float... elements) {
229 Objects.requireNonNull(elements, "elements is null");
230 return ofAll(io.vavr.collection.List.ofAll(elements));
231 }
232
233
234
235
236
237
238
239
240 public static Queue<Integer> ofAll(int... elements) {
241 Objects.requireNonNull(elements, "elements is null");
242 return ofAll(io.vavr.collection.List.ofAll(elements));
243 }
244
245
246
247
248
249
250
251
252 public static Queue<Long> ofAll(long... elements) {
253 Objects.requireNonNull(elements, "elements is null");
254 return ofAll(io.vavr.collection.List.ofAll(elements));
255 }
256
257
258
259
260
261
262
263
264 public static Queue<Short> ofAll(short... elements) {
265 Objects.requireNonNull(elements, "elements is null");
266 return ofAll(io.vavr.collection.List.ofAll(elements));
267 }
268
269
270
271
272
273
274
275
276
277
278
279 public static <T> Queue<T> tabulate(int n, Function<? super Integer, ? extends T> f) {
280 Objects.requireNonNull(f, "f is null");
281 return io.vavr.collection.Collections.tabulate(n, f, empty(), Queue::of);
282 }
283
284
285
286
287
288
289
290
291
292
293 public static <T> Queue<T> fill(int n, Supplier<? extends T> s) {
294 Objects.requireNonNull(s, "s is null");
295 return io.vavr.collection.Collections.fill(n, s, empty(), Queue::of);
296 }
297
298 public static Queue<Character> range(char from, char toExclusive) {
299 return ofAll(Iterator.range(from, toExclusive));
300 }
301
302 public static Queue<Character> rangeBy(char from, char toExclusive, int step) {
303 return ofAll(Iterator.rangeBy(from, toExclusive, step));
304 }
305
306 @GwtIncompatible
307 public static Queue<Double> rangeBy(double from, double toExclusive, double step) {
308 return ofAll(Iterator.rangeBy(from, toExclusive, step));
309 }
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327 public static Queue<Integer> range(int from, int toExclusive) {
328 return ofAll(Iterator.range(from, toExclusive));
329 }
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353 public static Queue<Integer> rangeBy(int from, int toExclusive, int step) {
354 return ofAll(Iterator.rangeBy(from, toExclusive, step));
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 public static Queue<Long> range(long from, long toExclusive) {
374 return ofAll(Iterator.range(from, toExclusive));
375 }
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 public static Queue<Long> rangeBy(long from, long toExclusive, long step) {
400 return ofAll(Iterator.rangeBy(from, toExclusive, step));
401 }
402
403 public static Queue<Character> rangeClosed(char from, char toInclusive) {
404 return ofAll(Iterator.rangeClosed(from, toInclusive));
405 }
406
407 public static Queue<Character> rangeClosedBy(char from, char toInclusive, int step) {
408 return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
409 }
410
411 @GwtIncompatible
412 public static Queue<Double> rangeClosedBy(double from, double toInclusive, double step) {
413 return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
414 }
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432 public static Queue<Integer> rangeClosed(int from, int toInclusive) {
433 return ofAll(Iterator.rangeClosed(from, toInclusive));
434 }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458 public static Queue<Integer> rangeClosedBy(int from, int toInclusive, int step) {
459 return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
460 }
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478 public static Queue<Long> rangeClosed(long from, long toInclusive) {
479 return ofAll(Iterator.rangeClosed(from, toInclusive));
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495 public static <T> Queue<Queue<T>> transpose(Queue<Queue<T>> matrix) {
496 return io.vavr.collection.Collections.transpose(matrix, Queue::ofAll, Queue::of);
497 }
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521 public static Queue<Long> rangeClosedBy(long from, long toInclusive, long step) {
522 return ofAll(Iterator.rangeClosedBy(from, toInclusive, step));
523 }
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550 public static <T, U> Queue<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f) {
551 return Iterator.unfoldRight(seed, f).toQueue();
552 }
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579 public static <T, U> Queue<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
580 return Iterator.unfoldLeft(seed, f).toQueue();
581 }
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607 public static <T> Queue<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
608 return Iterator.unfold(seed, f).toQueue();
609 }
610
611
612
613
614
615
616
617 @Override
618 public Queue<T> enqueue(T element) {
619 return new Queue<>(front, rear.prepend(element));
620 }
621
622
623
624
625
626
627
628
629
630 @SuppressWarnings("unchecked")
631 public Queue<T> enqueueAll(Iterable<? extends T> elements) {
632 Objects.requireNonNull(elements, "elements is null");
633 if (isEmpty() && elements instanceof Queue) {
634 return (Queue<T>) elements;
635 } else {
636 return io.vavr.collection.List.ofAll(elements).foldLeft(this, Queue::enqueue);
637 }
638 }
639
640
641
642 @Override
643 public Queue<T> append(T element) {
644 return enqueue(element);
645 }
646
647 @Override
648 public Queue<T> appendAll(Iterable<? extends T> elements) {
649 return enqueueAll(elements);
650 }
651
652 @Override
653 public java.util.List<T> asJava() {
654 return JavaConverters.asJava(this, IMMUTABLE);
655 }
656
657 @Override
658 public Queue<T> asJava(Consumer<? super java.util.List<T>> action) {
659 return Collections.asJava(this, action, IMMUTABLE);
660 }
661
662 @Override
663 public java.util.List<T> asJavaMutable() {
664 return JavaConverters.asJava(this, MUTABLE);
665 }
666
667 @Override
668 public Queue<T> asJavaMutable(Consumer<? super java.util.List<T>> action) {
669 return Collections.asJava(this, action, MUTABLE);
670 }
671
672 @Override
673 public <R> Queue<R> collect(PartialFunction<? super T, ? extends R> partialFunction) {
674 return ofAll(iterator().<R> collect(partialFunction));
675 }
676
677 @Override
678 public Queue<Queue<T>> combinations() {
679 return ofAll(toList().combinations().map(Queue::ofAll));
680 }
681
682 @Override
683 public Queue<Queue<T>> combinations(int k) {
684 return ofAll(toList().combinations(k).map(Queue::ofAll));
685 }
686
687 @Override
688 public Iterator<Queue<T>> crossProduct(int power) {
689 return io.vavr.collection.Collections.crossProduct(empty(), this, power);
690 }
691
692 @Override
693 public Queue<T> distinct() {
694 return ofAll(toList().distinct());
695 }
696
697 @Override
698 public Queue<T> distinctBy(Comparator<? super T> comparator) {
699 Objects.requireNonNull(comparator, "comparator is null");
700 return ofAll(toList().distinctBy(comparator));
701 }
702
703 @Override
704 public <U> Queue<T> distinctBy(Function<? super T, ? extends U> keyExtractor) {
705 Objects.requireNonNull(keyExtractor, "keyExtractor is null");
706 return ofAll(toList().distinctBy(keyExtractor));
707 }
708
709 @Override
710 public Queue<T> drop(int n) {
711 if (n <= 0) {
712 return this;
713 }
714 if (n >= length()) {
715 return empty();
716 }
717 return new Queue<>(front.drop(n), rear.dropRight(n - front.length()));
718 }
719
720 @Override
721 public Queue<T> dropUntil(Predicate<? super T> predicate) {
722 return io.vavr.collection.Collections.dropUntil(this, predicate);
723 }
724
725 @Override
726 public Queue<T> dropWhile(Predicate<? super T> predicate) {
727 Objects.requireNonNull(predicate, "predicate is null");
728 return dropUntil(predicate.negate());
729 }
730
731 @Override
732 public Queue<T> dropRight(int n) {
733 if (n <= 0) {
734 return this;
735 }
736 if (n >= length()) {
737 return empty();
738 }
739 return new Queue<>(front.dropRight(n - rear.length()), rear.drop(n));
740 }
741
742 @Override
743 public Queue<T> dropRightUntil(Predicate<? super T> predicate) {
744 return io.vavr.collection.Collections.dropUntil(reverse(), predicate).reverse();
745 }
746
747 @Override
748 public Queue<T> dropRightWhile(Predicate<? super T> predicate) {
749 Objects.requireNonNull(predicate, "predicate is null");
750 return dropRightUntil(predicate.negate());
751 }
752
753 @Override
754 public Queue<T> filter(Predicate<? super T> predicate) {
755 Objects.requireNonNull(predicate, "predicate is null");
756 final io.vavr.collection.List<T> filtered = toList().filter(predicate);
757
758 if (filtered.isEmpty()) {
759 return empty();
760 } else if (filtered.length() == length()) {
761 return this;
762 } else {
763 return ofAll(filtered);
764 }
765 }
766
767 @Override
768 public <U> Queue<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) {
769 Objects.requireNonNull(mapper, "mapper is null");
770 if (isEmpty()) {
771 return empty();
772 } else {
773 return new Queue<>(front.flatMap(mapper), rear.flatMap(mapper));
774 }
775 }
776
777 @Override
778 public T get(int index) {
779 if (isEmpty()) {
780 throw new IndexOutOfBoundsException("get(" + index + ") on empty Queue");
781 }
782 if (index < 0) {
783 throw new IndexOutOfBoundsException("get(" + index + ")");
784 }
785 final int length = front.length();
786 if (index < length) {
787 return front.get(index);
788 } else {
789 final int rearIndex = index - length;
790 final int rearLength = rear.length();
791 if (rearIndex < rearLength) {
792 final int reverseRearIndex = rearLength - rearIndex - 1;
793 return rear.get(reverseRearIndex);
794 } else {
795 throw new IndexOutOfBoundsException("get(" + index + ") on Queue of length " + length());
796 }
797 }
798 }
799
800 @Override
801 public <C> Map<C, Queue<T>> groupBy(Function<? super T, ? extends C> classifier) {
802 return io.vavr.collection.Collections.groupBy(this, classifier, Queue::ofAll);
803 }
804
805 @Override
806 public Iterator<Queue<T>> grouped(int size) {
807 return sliding(size, size);
808 }
809
810 @Override
811 public boolean hasDefiniteSize() {
812 return true;
813 }
814
815 @Override
816 public T head() {
817 if (isEmpty()) {
818 throw new NoSuchElementException("head of empty " + stringPrefix());
819 } else {
820 return front.head();
821 }
822 }
823
824 @Override
825 public int indexOf(T element, int from) {
826 final int frontIndex = front.indexOf(element, from);
827 if (frontIndex != -1) {
828 return frontIndex;
829 } else {
830
831 final int rearIndex = rear.reverse().indexOf(element, from - front.length());
832 return (rearIndex == -1) ? -1 : rearIndex + front.length();
833 }
834 }
835
836 @Override
837 public Queue<T> init() {
838 if (isEmpty()) {
839 throw new UnsupportedOperationException("init of empty " + stringPrefix());
840 } else if (rear.isEmpty()) {
841 return new Queue<>(front.init(), rear);
842 } else {
843 return new Queue<>(front, rear.tail());
844 }
845 }
846
847 @Override
848 public Queue<T> insert(int index, T element) {
849 if (index < 0) {
850 throw new IndexOutOfBoundsException("insert(" + index + ", element)");
851 }
852 final int length = front.length();
853 if (index <= length) {
854 return new Queue<>(front.insert(index, element), rear);
855 } else {
856 final int rearIndex = index - length;
857 final int rearLength = rear.length();
858 if (rearIndex <= rearLength) {
859 final int reverseRearIndex = rearLength - rearIndex;
860 return new Queue<>(front, rear.insert(reverseRearIndex, element));
861 } else {
862 throw new IndexOutOfBoundsException("insert(" + index + ", element) on Queue of length " + length());
863 }
864 }
865 }
866
867 @SuppressWarnings("unchecked")
868 @Override
869 public Queue<T> insertAll(int index, Iterable<? extends T> elements) {
870 Objects.requireNonNull(elements, "elements is null");
871 if (index < 0) {
872 throw new IndexOutOfBoundsException("insertAll(" + index + ", elements)");
873 }
874 final int length = front.length();
875 if (index <= length) {
876 if (isEmpty() && elements instanceof Queue) {
877 return (Queue<T>) elements;
878 } else {
879 final io.vavr.collection.List<T> newFront = front.insertAll(index, elements);
880 return (newFront == front) ? this : new Queue<>(newFront, rear);
881 }
882 } else {
883 final int rearIndex = index - length;
884 final int rearLength = rear.length();
885 if (rearIndex <= rearLength) {
886 final int reverseRearIndex = rearLength - rearIndex;
887 final io.vavr.collection.List<T> newRear = rear.insertAll(reverseRearIndex, io.vavr.collection.List.ofAll(elements).reverse());
888 return (newRear == rear) ? this : new Queue<>(front, newRear);
889 } else {
890 throw new IndexOutOfBoundsException("insertAll(" + index + ", elements) on Queue of length " + length());
891 }
892 }
893 }
894
895 @Override
896 public Queue<T> intersperse(T element) {
897 if (isEmpty()) {
898 return this;
899 } else if (rear.isEmpty()) {
900 return new Queue<>(front.intersperse(element), rear);
901 } else {
902 return new Queue<>(front.intersperse(element), rear.intersperse(element).append(element));
903 }
904 }
905
906
907
908
909
910
911 @Override
912 public boolean isAsync() {
913 return false;
914 }
915
916 @Override
917 public boolean isEmpty() {
918 return front.isEmpty();
919 }
920
921
922
923
924
925
926 @Override
927 public boolean isLazy() {
928 return false;
929 }
930
931 @Override
932 public boolean isTraversableAgain() {
933 return true;
934 }
935
936 @Override
937 public int lastIndexOf(T element, int end) {
938 return toList().lastIndexOf(element, end);
939 }
940
941 @Override
942 public int length() {
943 return front.length() + rear.length();
944 }
945
946 @Override
947 public <U> Queue<U> map(Function<? super T, ? extends U> mapper) {
948 Objects.requireNonNull(mapper, "mapper is null");
949 return new Queue<>(front.map(mapper), rear.map(mapper));
950 }
951
952 @Override
953 public Queue<T> orElse(Iterable<? extends T> other) {
954 return isEmpty() ? ofAll(other) : this;
955 }
956
957 @Override
958 public Queue<T> orElse(Supplier<? extends Iterable<? extends T>> supplier) {
959 return isEmpty() ? ofAll(supplier.get()) : this;
960 }
961
962 @Override
963 public Queue<T> padTo(int length, T element) {
964 final int actualLength = length();
965 if (length <= actualLength) {
966 return this;
967 } else {
968 return ofAll(toList().padTo(length, element));
969 }
970 }
971
972 @Override
973 public Queue<T> leftPadTo(int length, T element) {
974 final int actualLength = length();
975 if (length <= actualLength) {
976 return this;
977 } else {
978 return ofAll(toList().leftPadTo(length, element));
979 }
980 }
981
982 @Override
983 public Queue<T> patch(int from, Iterable<? extends T> that, int replaced) {
984 from = from < 0 ? 0 : from;
985 replaced = replaced < 0 ? 0 : replaced;
986 Queue<T> result = take(from).appendAll(that);
987 from += replaced;
988 result = result.appendAll(drop(from));
989 return result;
990 }
991
992 @Override
993 public Tuple2<Queue<T>, Queue<T>> partition(Predicate<? super T> predicate) {
994 Objects.requireNonNull(predicate, "predicate is null");
995 return toList().partition(predicate).map(io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue);
996 }
997
998 @Override
999 public Queue<Queue<T>> permutations() {
1000 return ofAll(toList().permutations().map(io.vavr.collection.List::toQueue));
1001 }
1002
1003 @Override
1004 public Queue<T> prepend(T element) {
1005 return new Queue<>(front.prepend(element), rear);
1006 }
1007
1008 @SuppressWarnings("unchecked")
1009 @Override
1010 public Queue<T> prependAll(Iterable<? extends T> elements) {
1011 Objects.requireNonNull(elements, "elements is null");
1012 if (isEmpty() && elements instanceof Queue) {
1013 return (Queue<T>) elements;
1014 } else {
1015 final io.vavr.collection.List<T> newFront = front.prependAll(elements);
1016 return (newFront == front) ? this : new Queue<>(newFront, rear);
1017 }
1018 }
1019
1020 @Override
1021 public Queue<T> remove(T element) {
1022 final io.vavr.collection.List<T> removed = toList().remove(element);
1023 return ofAll(removed.length() == length() ? this : removed);
1024 }
1025
1026 @Override
1027 public Queue<T> removeFirst(Predicate<T> predicate) {
1028 final io.vavr.collection.List<T> removed = toList().removeFirst(predicate);
1029 return ofAll(removed.length() == length() ? this : removed);
1030 }
1031
1032 @Override
1033 public Queue<T> removeLast(Predicate<T> predicate) {
1034 final io.vavr.collection.List<T> removed = toList().removeLast(predicate);
1035 return ofAll(removed.length() == length() ? this : removed);
1036 }
1037
1038 @Override
1039 public Queue<T> removeAt(int index) {
1040 return ofAll(toList().removeAt(index));
1041 }
1042
1043 @Override
1044 public Queue<T> removeAll(T element) {
1045 return io.vavr.collection.Collections.removeAll(this, element);
1046 }
1047
1048 @Override
1049 public Queue<T> replace(T currentElement, T newElement) {
1050 final io.vavr.collection.List<T> newFront = front.replace(currentElement, newElement);
1051 final io.vavr.collection.List<T> newRear = rear.replace(currentElement, newElement);
1052 return newFront.size() + newRear.size() == 0 ? empty()
1053 : newFront == front && newRear == rear ? this
1054 : new Queue<>(newFront, newRear);
1055 }
1056
1057 @Override
1058 public Queue<T> replaceAll(T currentElement, T newElement) {
1059 final io.vavr.collection.List<T> newFront = front.replaceAll(currentElement, newElement);
1060 final io.vavr.collection.List<T> newRear = rear.replaceAll(currentElement, newElement);
1061 return newFront.size() + newRear.size() == 0 ? empty()
1062 : newFront == front && newRear == rear ? this
1063 : new Queue<>(newFront, newRear);
1064 }
1065
1066 @Override
1067 public Queue<T> reverse() {
1068 return isEmpty() ? this : ofAll(toList().reverse());
1069 }
1070
1071 @Override
1072 public Queue<T> scan(T zero, BiFunction<? super T, ? super T, ? extends T> operation) {
1073 return scanLeft(zero, operation);
1074 }
1075
1076 @Override
1077 public <U> Queue<U> scanLeft(U zero, BiFunction<? super U, ? super T, ? extends U> operation) {
1078 return io.vavr.collection.Collections.scanLeft(this, zero, operation, Iterator::toQueue);
1079 }
1080
1081 @Override
1082 public <U> Queue<U> scanRight(U zero, BiFunction<? super T, ? super U, ? extends U> operation) {
1083 return io.vavr.collection.Collections.scanRight(this, zero, operation, Iterator::toQueue);
1084 }
1085
1086 @Override
1087 public Queue<T> shuffle() {
1088 return io.vavr.collection.Collections.shuffle(this, Queue::ofAll);
1089 }
1090
1091 @Override
1092 public Queue<T> slice(int beginIndex, int endIndex) {
1093 return ofAll(toList().slice(beginIndex, endIndex));
1094 }
1095
1096 @Override
1097 public Iterator<Queue<T>> slideBy(Function<? super T, ?> classifier) {
1098 return iterator().slideBy(classifier).map(Queue::ofAll);
1099 }
1100
1101 @Override
1102 public Iterator<Queue<T>> sliding(int size) {
1103 return sliding(size, 1);
1104 }
1105
1106 @Override
1107 public Iterator<Queue<T>> sliding(int size, int step) {
1108 return iterator().sliding(size, step).map(Queue::ofAll);
1109 }
1110
1111 @Override
1112 public Queue<T> sorted() {
1113 return ofAll(toList().sorted());
1114 }
1115
1116 @Override
1117 public Queue<T> sorted(Comparator<? super T> comparator) {
1118 Objects.requireNonNull(comparator, "comparator is null");
1119 return ofAll(toList().sorted(comparator));
1120 }
1121
1122 @Override
1123 public <U extends Comparable<? super U>> Queue<T> sortBy(Function<? super T, ? extends U> mapper) {
1124 return sortBy(U::compareTo, mapper);
1125 }
1126
1127 @Override
1128 public <U> Queue<T> sortBy(Comparator<? super U> comparator, Function<? super T, ? extends U> mapper) {
1129 final Function<? super T, ? extends U> domain = Function1.of(mapper::apply).memoized();
1130 return toJavaStream()
1131 .sorted((e1, e2) -> comparator.compare(domain.apply(e1), domain.apply(e2)))
1132 .collect(collector());
1133 }
1134
1135 @Override
1136 public Tuple2<Queue<T>, Queue<T>> span(Predicate<? super T> predicate) {
1137 Objects.requireNonNull(predicate, "predicate is null");
1138 return toList().span(predicate).map(io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue);
1139 }
1140
1141 @Override
1142 public Tuple2<Queue<T>, Queue<T>> splitAt(int n) {
1143 return toList().splitAt(n).map(io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue);
1144 }
1145
1146 @Override
1147 public Tuple2<Queue<T>, Queue<T>> splitAt(Predicate<? super T> predicate) {
1148 return toList().splitAt(predicate).map(io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue);
1149 }
1150
1151 @Override
1152 public Tuple2<Queue<T>, Queue<T>> splitAtInclusive(Predicate<? super T> predicate) {
1153 return toList().splitAtInclusive(predicate).map(io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue);
1154 }
1155
1156 @Override
1157 public boolean startsWith(Iterable<? extends T> that, int offset) {
1158 return toList().startsWith(that, offset);
1159 }
1160
1161 @Override
1162 public Queue<T> subSequence(int beginIndex) {
1163 if (beginIndex < 0 || beginIndex > length()) {
1164 throw new IndexOutOfBoundsException("subSequence(" + beginIndex + ")");
1165 } else {
1166 return drop(beginIndex);
1167 }
1168 }
1169
1170 @Override
1171 public Queue<T> subSequence(int beginIndex, int endIndex) {
1172 Collections.subSequenceRangeCheck(beginIndex, endIndex, length());
1173 if (beginIndex == endIndex) {
1174 return empty();
1175 } else if (beginIndex == 0 && endIndex == length()) {
1176 return this;
1177 } else {
1178 return ofAll(toList().subSequence(beginIndex, endIndex));
1179 }
1180 }
1181
1182 @Override
1183 public Queue<T> tail() {
1184 if (isEmpty()) {
1185 throw new UnsupportedOperationException("tail of empty " + stringPrefix());
1186 } else {
1187 return new Queue<>(front.tail(), rear);
1188 }
1189 }
1190
1191 @Override
1192 public Queue<T> take(int n) {
1193 if (n <= 0) {
1194 return empty();
1195 }
1196 if (n >= length()) {
1197 return this;
1198 }
1199 final int frontLength = front.length();
1200 if (n < frontLength) {
1201 return new Queue<>(front.take(n), io.vavr.collection.List.empty());
1202 } else if (n == frontLength) {
1203 return new Queue<>(front, io.vavr.collection.List.empty());
1204 } else {
1205 return new Queue<>(front, rear.takeRight(n - frontLength));
1206 }
1207 }
1208
1209 @Override
1210 public Queue<T> takeRight(int n) {
1211 if (n <= 0) {
1212 return empty();
1213 }
1214 if (n >= length()) {
1215 return this;
1216 }
1217 final int rearLength = rear.length();
1218 if (n < rearLength) {
1219 return new Queue<>(rear.take(n).reverse(), io.vavr.collection.List.empty());
1220 } else if (n == rearLength) {
1221 return new Queue<>(rear.reverse(), io.vavr.collection.List.empty());
1222 } else {
1223 return new Queue<>(front.takeRight(n - rearLength), rear);
1224 }
1225 }
1226
1227 @Override
1228 public Queue<T> takeUntil(Predicate<? super T> predicate) {
1229 Objects.requireNonNull(predicate, "predicate is null");
1230 final io.vavr.collection.List<T> taken = toList().takeUntil(predicate);
1231 return taken.length() == length() ? this : ofAll(taken);
1232 }
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242 public <U> U transform(Function<? super Queue<T>, ? extends U> f) {
1243 Objects.requireNonNull(f, "f is null");
1244 return f.apply(this);
1245 }
1246
1247 @Override
1248 public <T1, T2> Tuple2<Queue<T1>, Queue<T2>> unzip(
1249 Function<? super T, Tuple2<? extends T1, ? extends T2>> unzipper) {
1250 Objects.requireNonNull(unzipper, "unzipper is null");
1251 return toList().unzip(unzipper).map(io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue);
1252 }
1253
1254 @Override
1255 public <T1, T2, T3> Tuple3<Queue<T1>, Queue<T2>, Queue<T3>> unzip3(Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>> unzipper) {
1256 Objects.requireNonNull(unzipper, "unzipper is null");
1257 return toList().unzip3(unzipper).map(io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue, io.vavr.collection.List::toQueue);
1258 }
1259
1260 @Override
1261 public Queue<T> update(int index, T element) {
1262 return ofAll(toList().update(index, element));
1263 }
1264
1265 @Override
1266 public Queue<T> update(int index, Function<? super T, ? extends T> updater) {
1267 Objects.requireNonNull(updater, "updater is null");
1268 return update(index, updater.apply(get(index)));
1269 }
1270
1271 @Override
1272 public <U> Queue<Tuple2<T, U>> zip(Iterable<? extends U> that) {
1273 return zipWith(that, Tuple::of);
1274 }
1275
1276 @SuppressWarnings("unchecked")
1277 @Override
1278 public <U, R> Queue<R> zipWith(Iterable<? extends U> that, BiFunction<? super T, ? super U, ? extends R> mapper) {
1279 Objects.requireNonNull(that, "that is null");
1280 Objects.requireNonNull(mapper, "mapper is null");
1281 return ofAll(toList().zipWith(that, mapper));
1282 }
1283
1284 @Override
1285 public <U> Queue<Tuple2<T, U>> zipAll(Iterable<? extends U> that, T thisElem, U thatElem) {
1286 Objects.requireNonNull(that, "that is null");
1287 return ofAll(toList().zipAll(that, thisElem, thatElem));
1288 }
1289
1290 @Override
1291 public Queue<Tuple2<T, Integer>> zipWithIndex() {
1292 return zipWithIndex(Tuple::of);
1293 }
1294
1295 @Override
1296 public <U> Queue<U> zipWithIndex(BiFunction<? super T, ? super Integer, ? extends U> mapper) {
1297 Objects.requireNonNull(mapper, "mapper is null");
1298 return ofAll(toList().zipWithIndex(mapper));
1299 }
1300
1301 private Object readResolve() {
1302 return isEmpty() ? EMPTY : this;
1303 }
1304
1305 @Override
1306 public String stringPrefix() {
1307 return "Queue";
1308 }
1309
1310 @Override
1311 public boolean equals(Object o) {
1312 return io.vavr.collection.Collections.equals(this, o);
1313 }
1314
1315 @Override
1316 public int hashCode() {
1317 return io.vavr.collection.Collections.hashOrdered(this);
1318 }
1319 }